{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a caesar cipher\n",
    "\n",
    "### Caesar ciphers\n",
    "\n",
    "A caesar cipher is an encryption scheme that shifts all letters in the alphabet by some specified offset to other letters in the alphabet.\n",
    "\n",
    "For example, a shift of 1 would turn the letter \"A\" into the letter \"B\" and the letter \"M\" to the letter \"N\".\n",
    "\n",
    "### Goal\n",
    "\n",
    "We want to add a method to the `+` operator such that we can add together a string and an integer shift to encrypt a message. For example,\n",
    "\n",
    "```julia\n",
    "4 + \"hello\" == \"lipps\"\n",
    "```\n",
    "\n",
    "We'll walk you through the steps to do this!\n",
    "\n",
    "### Test it out\n",
    "\n",
    "Once you think you have it working, try to decrypt the following string by adding a shift of -7.\n",
    "```julia\n",
    "\"Kv'uv{'tlkksl'pu'{ol'hmmhpyz'vm'kyhnvuz'mvy'\\u80v|'hyl'jy|ujo\\u80'huk'{hz{l'nvvk'~p{o'rl{jo|w5\"\n",
    "```\n",
    "\n",
    "### Let's get started!\n",
    "\n",
    "First, we want a way to convert between characters and integers. Actually, under the hood, all of our characters are being represented as numbers via their *ASCII representation*.\n",
    "\n",
    "You can start to get a feel for how this works by running the following lines of code.\n",
    "\n",
    "```julia\n",
    "convert(Int, 'a')\n",
    "convert(Int, 'b')\n",
    "convert(Char, 97)\n",
    "convert(Char, 98)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What happens when you try to add an integer to a character? (Note that the difference between `Char`s and `String`s is important here!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When we treat a string elementwise, what is the type of (`typeof`) each element?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try to write a function called `caesar(shift, stringin)` that encodes its input string, `stringin`, by shifting all letters in the alphabet by `shift`.\n",
    "\n",
    "One way to do this is to use the `map` or `broadcast` function!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you think you have this working, try out\n",
    "```julia\n",
    "caesar(-4, \"lipps\")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Last, we want to extend the `+` operator to include a way to apply this cipher.\n",
    "\n",
    "The `+` operator lives in a place called \"Base\". Everything that lives in Base is accessible to us as users by default, but we need a special incantation to modify the things that live in Base. If we want to modify `+`, our incantation is\n",
    "\n",
    "```julia\n",
    "import Base: +\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After you have imported `+` from Base, we are ready to modify it.\n",
    "\n",
    "If you're not sure how to add a method for `+`, let's go through an example first.\n",
    "\n",
    "Let's say we want to add a method for `+` that allows us to concatenate strings so that we can say\n",
    "```julia\n",
    "\"hello \" + \"world\"\n",
    "```\n",
    "and receive the output string ```hello world```.\n",
    "\n",
    "To do this, we can write a method for `+` like this"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "+(x::String, y::String) = string(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that I've added type information about `x` and `y` with a double colon and the word `String` to say that `x` and `y` are both strings.\n",
    "\n",
    "To test that this works, try out \n",
    "```julia\n",
    "\"hello \" + \"world\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now that you've extended `+` once, let's add another method for `+` that calls the `caesar` function we've written."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now try it out via\n",
    "\n",
    "```julia\n",
    "-7 + \"Kv'uv{'tlkksl'pu'{ol'hmmhpyz'vm'kyhnvuz'mvy'\\u80v|'hyl'jy|ujo\\u80'huk'{hz{l'nvvk'~p{o'rl{jo|w5\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.0",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
